package com.css.fxfzxczh.modules.remoteSenseData.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.css.fxfzxczh.base.dict.service.DictService;
import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.remoteSenseData.entity.RemoteSense;
import com.css.fxfzxczh.modules.remoteSenseData.entity.RemoteSenseSupplement;
import com.css.fxfzxczh.modules.remoteSenseData.repository.RemoteSenseRepository;
import com.css.fxfzxczh.modules.remoteSenseData.repository.RemoteSenseSupplementRepository;
import com.css.fxfzxczh.modules.remoteSenseData.repository.impl.remoteSenseSupplementRepositoryImpl;
import com.css.fxfzxczh.modules.remoteSenseData.service.RemoteSenseDataService;

import com.css.fxfzxczh.modules.remoteSenseData.vo.ImportExcelVo;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.EarEveManage;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.service.EarEveManageService;
import com.css.fxfzxczh.util.UUIDGenerator;
import com.css.fxfzxczh.web.PlatformSessionContext;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author:
 * @CreateTime: 2024/3/11 13:55
 * @Description:
 */
@Service
public class RemoteSenseDataServiceImpl implements RemoteSenseDataService {
    @Resource
    EarEveManageService earEveManageService;

    @Resource
    RemoteSenseRepository remoteSenseRepository;

    @Resource
    RemoteSenseSupplementRepository remoteSenseSupplementRepository;

    @Resource
    DictService dictService;

    @Override
    public Map<String, Object> getPage(int curPage, int pageSize,String xsemId) {
        Map<String,Object> page=remoteSenseRepository.getPage(curPage,pageSize,xsemId);
        return page;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void importExcel(MultipartFile file) throws Exception {
        // 导入的参数信息
        ImportParams params = new ImportParams();
        params.setTitleRows(0);
        params.setHeadRows(1);
        //params.setImportFields(new String[]{"省","市","县","姓名","性别","类别","角色","单位名称","手机号","描述信息"});
        //params.setNeedVerify(true);
        //params.setVerifyHandler();//校验
        List<RemoteSense> remoteSenseList=new ArrayList<>();
        List<RemoteSenseSupplement> remoteSenseSupplementList=new ArrayList<>();


            ExcelImportResult<ImportExcelVo> result = ExcelImportUtil.importExcelMore(file.getInputStream(), ImportExcelVo.class, params);
            List<ImportExcelVo> list = result.getList();
            int batchSize = 10;
            int totalSize = list.size();
            String userID = PlatformSessionContext.getUserID();
            EarEveManage currentEvent = earEveManageService.getCurrentEvent();
            String xsemId = currentEvent.getXsemId();
//            String xsemId = "xsemid";
//            userID="userid";
            // 外部循环每隔 batchSize 条数据获取第一条
            for (int i = 0; i < totalSize; i += batchSize) {
                ImportExcelVo importExcelVo = list.get(i);
                RemoteSense remoteSense = new RemoteSense();
                BeanUtils.copyProperties(importExcelVo, remoteSense);
                String id = UUIDGenerator.getUUID();
                Date date = new Date();

                remoteSense.setId(id);
                remoteSense.setCreateTime(date);
                remoteSense.setCreateUser(userID);
                remoteSense.setXsemId(xsemId);
                remoteSense.setDelFlag(YNEnum.N.toString());
                remoteSenseList.add(remoteSense);
                // 内部循环获取当前索引开始的 batchSize 条数据
                for (int j = i; j < i + batchSize && j < totalSize; j++) {
                    ImportExcelVo importExcelVo1 = list.get(j);
                    RemoteSenseSupplement remoteSenseSupplement= new RemoteSenseSupplement();
                    BeanUtils.copyProperties(importExcelVo1, remoteSenseSupplement);


                    remoteSenseSupplement.setStructureType(dictService.getKeyByDictCodeAndValue("jglx",remoteSenseSupplement.getStructureType() ));
                    remoteSenseSupplement.setId(UUIDGenerator.getUUID());
                    remoteSenseSupplement.setXrsId(id);
                    remoteSenseSupplementList.add(remoteSenseSupplement);
                }
            }
            remoteSenseRepository.batchSave(remoteSenseList);
            remoteSenseSupplementRepository.batchSave(remoteSenseSupplementList);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void delete(List<String> ids) {
        remoteSenseRepository.delete(ids);
        remoteSenseSupplementRepository.delete(ids);
    }
}
